<?php

/**
 * Memcache,不要用new创建,而要用
 */
class core_memcache extends Memcache {
	private static $mCache;
	/**
	 * 配置Memcache
	 */
	function __construct() {
//		$server = getRunENV() == RUNENV_ONLINE ? "192.168.1.205" : '127.0.0.1';
		$server = '127.0.0.1';
		$this->connect($server, 11211);
	}

	/**
	 * core_Memcache
	 * @return core_Memcache
	 */
	public static function getInstance() {
		if(!self::$mCache) self::$mCache = new core_Memcache();
		return self::$mCache;
	}

	/**
	 * 保存整个问题信息
	 * @param array $question
	 */
	function setQuestion($id,$question) {
//		$this->set('question_'.$id, $question, 0, ini_get('session.gc_maxlifetime'));
		$this->set('question_'.$id, $question, 0, 0);
	}

	/**
	 * 保存单条问题信息
	 * @param array $question
	 */
	function setQuestionTable($id,$question) {
		$this->set('question_table_'.$id, $question, 0, 0);
	}

	/**
	 * 获取问题信息
	 * @param array $question_id
	 */
	function getQuestion($question_id) {
		$question = $this->get('question_'.$question_id);
		return $question;
	}

	/**
	 * 获取单条问题信息
	 * @param array $question_id
	 */
	function getQuestionTable($question_id) {
		$question = $this->get('question_table_'.$question_id);
		return $question;
	}

	/**
	 * 保存整个文章信息
	 * @param array $article
	 */
	function setArticle($id,$article) {
//		$this->set('article_'.$id, $article, 0, ini_get('session.gc_maxlifetime'));
		$this->set('article_'.$id, $article, 0, 0);
	}

	/**
	 * 保存单条文章信息
	 * @param array $article
	 */
	function setArticleTable($id,$article) {
//		$this->set('article_'.$id, $article, 0, ini_get('session.gc_maxlifetime'));
		$this->set('article_table_'.$id, $article, 0, 0);
	}

	/**
	 * 获取整个文章信息
	 * @param array $id
	 */
	function getArticle($id) {
		return $this->get('article_'.$id);
	}

	/**
	 * 获取单条文章信息
	 * @param array $id
	 */
	function getArticleTable($id) {
		return $this->get('article_table_'.$id);
	}

	/**
	 * 获取相应的Key
	 * @param int $type 1:打招呼,2:推送,3:在线,4:重置密码,5:发送短信验证码,6:竞争力名次,7:竞争力排行榜,8:机器人登陆时间,<br/>
	 * 		9:发红包,10:重置支付密码,11:异步消费通知,12:已推送过的用户编号,13:无动态发信息,14:会员推荐,15:注册女性用户数,<br/>
	 * 		16:领取话费名单,17:自动回复时间,18:领取话费名单1.26,19:话术轮循,20:领取话费名单1.27,21:话术1.27轮循,
	 * 		22:每日免费聊天数,23:每日自动回复数
	 * @param $var1=''
	 * @param $var2=''
	 */
	function getKey($type, $var1='', $var2='') {
		switch ($type) {
			case '1':return "msg_key_{$var1}_{$var2}";
			case '2':return "question3_{$var1}";
			case '3':return "online_{$var1}";
			case '4':return "resetPwd_{$var1}";
			case '5':return "sendSmsTimes_{$var1}";
			case '6':return "activityTopMy_{$var1}_{$var2}";
			case '7':return "activityTops_{$var1}_{$var2}";
			case '8':return "loginTime_{$var1}";
			case '9':return "redpacket_{$var1}";
			case '10':return "resetPayPwd_{$var1}";
			case '11':return "asyn_{$var1}";
			case '12':return "question3ids_{$var1}";
			case '13':return "emptyTalk_{$var1}_{$var2}";
			case '14':return "recommendList_{$var1}";
			case '15':return "registerGirls";
			case '16':return "getPhoneFeeList";
			case '18':return "getPhoneFeeList1.26";
			case '20':return "getPhoneFeeList1.27";
			case '24':return "getPhoneFeeList1.28";
			case '25':return "getPhoneFeeList1.58";
			case '17':return "autoRecievTime_{$var1}";
			case '19':return "messagesRound_{$var1}";
			case '21':return "messagesRound1.27_{$var1}";
			case '22':return "free_letter_{$var1}";
			case '23':return "autoReciev_{$var1}";
		}
	}

	/**
	 * 保存整个用户信息
	 * @param array $user
	 */
	function setUser($user, $init = true) {
		if($init) $user['synchro'] = 0;
		$this->set('userInfo_'.$user['uid'], $user, 0, ini_get('session.gc_maxlifetime'));
	}

	/**
	 * 更新部分用户信息
	 * @param int $uid
	 * @param array $user
	 * @param boolean $synchro=false 是否要同步
	 */
	function upUser($uid, $update, $synchro = false) {
		$user = $this->getUser($uid);
		if($user !== false) {
			foreach ($update as $k => $v) $user[$k] = $v;
			if($synchro) $user['synchro'] = 1;
			$this->setUser($user, false);
		}
	}

	/**
	 * 更新某项用户信息
	 * @param int $uid
	 * @param string $key
	 * @param string $value
	 * @param boolean $synchro=false 是否要同步
	 */
	function upUserKey($uid, $key, $value, $synchro = false) {
		$user = $this->getUser($uid);
		if($user !== false) {
			$user[$key] = $value;
			if($synchro) $user['synchro'] = 1;
			$this->setUser($user, false);
		}
	}

	/**
	 * 增加某项用户信息的值
	 * @param int $uid
	 * @param string $key
	 * @param int $value
	 * @param boolean $synchro=false 是否要同步
	 * @return false|int
	 */
	function incrUserKey($uid, $key, $value, $synchro = false) {
		$user = $this->getUser($uid);
		if($user !== false) {
			$user[$key] += $value;
			if($synchro) $user['synchro'] = 1;
			$this->setUser($user, false);
			return $user[$key];
		}
		return false;
	}

	/**
	 * 用户信息是否要同步
	 * @param $uid
	 * @return boolean true需要同步
	 */
	function needSynchro($uid) {
		$user = $this->getUser($uid);
		return $user['synchro'];
	}

	/**
	 * 关闭同步
	 * @param $uid
	 */
	function closeSynchro($uid) {
		$user = $this->getUser($uid);
		if($user !== false) {
			$user['synchro'] = 0;
			$this->setUser($user, false);
		}
	}

	/**
	 * 获取用户信息
	 * @param int $uid
	 * @param string $key='' 子键为空时返回整个用户数组
	 * @return array|string	不存在时返回false
	 */
	function getUser($uid, $key='') {
		$user = $this->get('userInfo_'.$uid);
		if($key && $user) return $user[$key];
		return $user;
	}

	/**
	 * 获取用户头像URL
	 * @return array (filename,ischeck)
	 */
	function getHeadImg($uid) {
		$filename = array('filename'=>'', 'ischeck'=>'');
		if($imgs = $this->getUser($uid, 'imgs')) foreach ($imgs as $v) {
			if($v['ishead']/* && $v['ischeck']*/)
				$filename = array('filename'=>$v['filename'], 'ischeck'=>$v['ischeck']);
		}
		return $filename;
	}

	/**
	 * 计算资料完成度并保存
	 * @return int
	 */
	function countInfoFinish($uid) {
		$array = array(
			'sex', 'birthday', 'height', 'weight', 'blood_type',
			'live_province', 'native_province', 'special', 'hobby', 'school',
			'job', 'income', 'charm', 'marriage', 'house',
			'yidi', 'loves_type', 'close', 'live_parent', 'kid'
		);
		$return = 0;
		$user = $this->getUser($uid);
		foreach ($array as $v) {
			if($user[$v]) $return += 5;
		}
		$this->upUserKey($uid, 'info_finish', $return);
		return $return;
	}

}